package com.gxa.modules.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxa.common.vo.JsonResult;
import com.gxa.common.vo.R;
import com.gxa.modules.mapper.ContractMapper;
import com.gxa.modules.pojo.Contract;
import com.gxa.modules.service.ContractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;


@Service
public class ContractServiceImpl extends ServiceImpl<ContractMapper, Contract> implements ContractService {

    @Autowired
    private ContractMapper contractMapper;

    @Override
    public JsonResult findContractList(Long currentPage, Long pageSize) {
        JsonResult jr=new JsonResult();
        try{
            Long start=(currentPage-1)*pageSize;
            Long off=pageSize;
            Page<Contract> page=new Page<>(start,off);
            Page<Contract> contractPage=contractMapper.selectPage(page,null);
            jr.setCode(200);
            jr.setMsg("查询成功");
            jr.setData(contractPage.getRecords());
            jr.setCount((int) contractPage.getTotal());
        }catch (Exception e){
            e.printStackTrace();
            jr.setCode(500);
            jr.setMsg("网络开小差");
        }
        return jr;
    }

    @Override
    public R queryContractById(Integer id) {
        R r =new R();
        try{
            Contract contract = contractMapper.selectById(id);
            r.setCode(200);
            r.setMsg("查询成功");
            r.setData(contract);
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("网络炸了");
        }
        return r;
    }

    @Override
    public JsonResult findContractListByCondition(Long currentPage, Long pageSize, String customer_name, String signed_time) {
        JsonResult jr=new JsonResult();
        try {
            Long start=(currentPage-1)*pageSize;
            Long off =pageSize;
            Page<Contract> page =new Page<>(start,off);
            QueryWrapper<Contract> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq(StringUtils.isNotBlank(customer_name),"customer_name",customer_name)
                        .eq(StringUtils.isNotBlank(signed_time),"signed_time",signed_time);
            Page<Contract> contractPage =contractMapper.selectPage(page,queryWrapper);
            jr.setCode(200);
            jr.setMsg("查询成功");
            jr.setCount((int) contractPage.getTotal());
            jr.setData(contractPage.getRecords());
        }catch (Exception e){
            e.printStackTrace();
            jr.setCode(500);
            jr.setMsg("网络炸了");
        }

        return jr;
    }

    @Override
    public R addContractPaper(Contract contract) {
        R r = new R();
        try{
            Date date = new Date();
            SimpleDateFormat format =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String datetime = format.format(date);
            contract.setContractTime(datetime);
            SimpleDateFormat format2 =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String datetime2 = format2.format(date);
            contract.setSignedTime(datetime2);
            int result = contractMapper.insert(contract);
            if (result>0){
                r.setCode(200);
                r.setMsg("添加成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("网络开小差");
        }
        return r;
    }

    @Override
    public R addContractPw(Contract contract) {
        R r = new R();
        try{
            Date date = new Date();
            SimpleDateFormat format =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String datetime = format.format(date);
            contract.setContractTime(datetime);
            SimpleDateFormat format2 =new SimpleDateFormat("yyyy-MM-dd");
            String datetime2 = format2.format(date);
            contract.setSignedTime(datetime2);
            int result = contractMapper.insert(contract);
            if (result>0){
                r.setCode(200);
                r.setMsg("添加成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("网络开小差");
        }
        return r;
    }

    @Override
    public R delContractById(Integer id) {
        R r = new R();
        try {
            int result = contractMapper.deleteById(id);
            if (result>0){
                r.setCode(200);
                r.setMsg("删除成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("网络开小差");
        }
        return r;
    }

}
